package net.cctms.zhishu.webAppFramework.global;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.material.MaterialProgressWheel;
import net.cctms.zhishu.webAppFramework.ui.CheckPermissionsActivity;
import net.cctms.zhishu.webAppFramework.utils.ToastUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * 类名 BaseFragmentActivity
 * 作者 Keyu Chan
 * 创建日期 2017/8/2
 * 邮箱 chenky@corp.21cn.com
 * 说明
 */
public class BaseFragmentActivity extends CheckPermissionsActivity {
    protected Context mContext = null;
    protected String TAG;
   // protected NewsApi mNewsApi;
    protected View mLoadingLayout;
    protected MaterialProgressWheel mLoadingView;
    protected TextView mLoadFailView;
    protected View mNoDataLayout;
    public String PAGE_FLAG = UUID.randomUUID().toString() ;
    public static int kCoinAmount,moneyAmount;
    public static boolean isAuth=false;//是否授权
    public static List<Activity> activities=new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //沙箱环境   若是线上环境需注释掉下面一行
        //EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        mContext = this;
        activities.add(this);
        //mNewsApi = RetrofitService.createApi(NewsApi.class);
        processMIUI(true, this);
    }

    @TargetApi(19)
    protected void setTranslucentStatus(boolean on, Activity act) {
        Window win = act.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void processMIUI(boolean lightStatusBar, Activity act) {
        try {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window win = act.getWindow();
                Class<? extends Window> clazz = win.getClass();
                int darkModeFlag;
                Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags",int.class,int.class);
                extraFlagField.invoke(win, lightStatusBar ? darkModeFlag : 0, darkModeFlag);
                win.setStatusBarColor(getResources().getColor(android.R.color.white));
            }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                window.setStatusBarColor(getResources().getColor(android.R.color.white));
            }
        } catch (Exception e) {
            processFlyMe(true, this);
        }
    }

    private void processFlyMe(boolean dark, Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                activity.getWindow().setStatusBarColor(getResources().getColor(android.R.color.white));
            } catch (Exception e) {
                //状态栏：白色背景，黑色字体
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    Window window = getWindow();
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(getResources().getColor(android.R.color.white));
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
            }
        }
    }

    protected void initSystemBar(Activity act) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true, act);
            SystemBarTintManager tintManager = new SystemBarTintManager(act);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.white);
        }
    }

    public String getPageFlag() {
        return PAGE_FLAG;
    }

    /**
     * 显示加载中
     */
    @Deprecated
    public void showLoading(){
        mLoadingLayout.setVisibility(View.VISIBLE);
        mLoadingView.setVisibility(View.VISIBLE);
        mLoadFailView.setVisibility(View.GONE);
        mNoDataLayout.setVisibility(View.GONE);
    }


    /**
     * 隐藏加载布局
     */
    @Deprecated
    public void hideLoadLayout(){
        mLoadingLayout.setVisibility(View.GONE);
        mLoadingView.setVisibility(View.GONE);
        mLoadFailView.setVisibility(View.GONE);
    }

    protected ProgressDialog progressDialog;

    public void showLoadingProgress() {
        showLoadingProgress("");
    }

    public void showLoadingProgress(String hint) {
        progressDialog = new ProgressDialog(this);
        if(!TextUtils.isEmpty(hint)){
            progressDialog.setMessage(hint);
        }else{
            progressDialog.setMessage(getResources().getString(R.string.common_waiting));
        }
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressDialog.setIndeterminate(true);
        progressDialog.show();
    }

    public void dismissLoadingProgress() {
        if(!isFinishing()){
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }

    /*
    public NewsApi getmNewsApi() {
        return mNewsApi;
    }
    */




    @Override
    protected void onDestroy() {
        super.onDestroy();

        activities.remove(this);
    }

    @Override
    public void finish() {
        super.finish();
    }

    public void finishActivity(){
        finish();
        //定义关闭Activity动画
        overridePendingTransition(R.anim.activity_notmove, R.anim.push_right_out);
    }

    @Override
    public void onBackPressed() {
        finishActivity();
    }

    /**
     * 网络不可用提示
     */
    public void showNetFailToast(){
        showShortToast(getResources().getString(R.string.net_not_available));
    }

    /**
     * 显示长Toast
     * @param tip
     */
    public  void showLongToast(String tip){
        ToastUtil.showLongToast(this, tip);
    }

    /**
     * 显示短Toast
     * @param tip
     */
    public  void showShortToast(String tip){
        ToastUtil.showShortToast(this, tip);
    }
}
